package com.hui.dbmigrator.util;

import com.hui.dbmigrator.config.DatabaseConfig;
import com.hui.dbmigrator.entity.DatabaseType;
import com.zaxxer.hikari.HikariDataSource;

import javax.sql.DataSource;

// DataSourceUtil.java
public class DataSourceUtil {

    public static DataSource createDataSource(DatabaseConfig config) {
        HikariDataSource ds = new HikariDataSource();
        ds.setJdbcUrl(config.getJdbcUrl());
        ds.setUsername(config.getUsername());
        ds.setPassword(config.getPassword());
        ds.setMaximumPoolSize(5);
        ds.setMinimumIdle(1);
        ds.setConnectionTimeout(30000);
        ds.setIdleTimeout(600000);
        ds.setMaxLifetime(1800000);

        switch (DatabaseType.valueOf(config.getType().getDisplayName().toUpperCase())) {
            case MYSQL:
                ds.setDriverClassName("com.mysql.cj.jdbc.Driver");
                break;
            case ORACLE:
                ds.setDriverClassName("oracle.jdbc.OracleDriver");
                break;
            case POSTGRESQL:
                ds.setDriverClassName("org.postgresql.Driver");
                break;
            case SQLSERVER:
                ds.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                break;
        }

        return ds;
    }
}
